package com.qms.repast.weidgets.cardview;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.qms.repast.ui.adapter.CardViewAdapter;

/**
 * @author 泅渡者
 * @date 2018/8/9 0009
 * @describe:
 */
public class PullDownCallBack extends ItemTouchHelper.Callback {
	protected RecyclerView mRv;
	protected CardViewAdapter mAdapter;
	protected IItemHelper iItemHelper;

	public PullDownCallBack (RecyclerView rv, CardViewAdapter adapter, IItemHelper iItemHelper) {
		this.mRv = rv;
		this.mAdapter = adapter;
		this.iItemHelper = iItemHelper;
	}

	@Override
	public int getMovementFlags (RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

		int dragFlags = 0;

		int swipeFlags = ItemTouchHelper.DOWN;

		return makeMovementFlags(dragFlags, swipeFlags);
	}

	/**
	 * 是否开启长按拖拽 ，必须开启
	 *
	 * @return
	 */
	@Override
	public boolean isLongPressDragEnabled () {
		return false;
	}

	/**
	 * 是否滑动
	 *
	 * @return
	 */
	@Override
	public boolean isItemViewSwipeEnabled () {
		return true;
	}

	/**
	 * 滑动排序
	 *
	 * @param recyclerView
	 * @param viewHolder
	 * @param target
	 *
	 * @return
	 */
	@Override
	public boolean onMove (RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
		iItemHelper.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
		return false;
	}

	@Override
	public void onSwiped (RecyclerView.ViewHolder viewHolder, int direction) {
		viewHolder.itemView.setOnTouchListener(null);
		//对rotate进行复位
		viewHolder.itemView.setRotation(0);
	}

	@Override
	public void onChildDraw (Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
		//仅对侧滑状态下的效果做出改变
		if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
			if (Math.abs(dY) >= recyclerView.getHeight() / 5 ) {
				iItemHelper.onItemSwipe(viewHolder.getAdapterPosition());
			}
		}
		super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

	}

	/**
	 * ItemView复位回调
	 *
	 * @param recyclerView
	 * @param viewHolder
	 */
	@Override
	public void clearView (RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
		super.clearView(recyclerView, viewHolder);
		viewHolder.itemView.setRotation(0f);
	}

	/**
	 * 禁止叠加
	 *
	 * @param recyclerView
	 * @param current
	 * @param target
	 *
	 * @return
	 */
	@Override
	public boolean canDropOver (RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target) {
		return false;
	}

	/**
	 * 返回值作为用户视为拖动的距离
	 *
	 * @param viewHolder
	 *
	 * @return
	 */
	@Override
	public float getMoveThreshold (RecyclerView.ViewHolder viewHolder) {
		return super.getMoveThreshold(viewHolder);
	}


	/**
	 * 返回值滑动消失的距离，滑动小于这个值不消失，大于消失
	 *
	 * @param defaultValue
	 *
	 * @return
	 */
	@Override
	public float getSwipeEscapeVelocity (float defaultValue) {
		return 2f;
	}


	/**
	 * 返回值滑动消失的距离, 这里是相对于RecycleView的宽度，0.5f表示为RecycleView的宽度的一半，取值为0~1f之间
	 *
	 * @param viewHolder
	 *
	 * @return
	 */
	@Override
	public float getSwipeThreshold (RecyclerView.ViewHolder viewHolder) {
		return 2f;
	}


	/**
	 * 返回值作为滑动的流程程度，越小越难滑动，越大越好滑动
	 *
	 * @param defaultValue
	 *
	 * @return
	 */
	@Override
	public float getSwipeVelocityThreshold (float defaultValue) {
		return 1f;
	}

}